package cn.lee.chp1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Request {
    //this is the request input
    InputStream in = null;
    
    // this request uri
    String uri;
    
    public Request(InputStream in) {
        this.in = in;
    }
    public void parse() throws IOException, InterruptedException{
        BufferedReader bf = new BufferedReader( new InputStreamReader(in));
        StringBuffer sb = new StringBuffer();
//        Thread.currentThread().sleep(50);
//        if(bf.ready()){
//            String line = null;
//            while( (line = bf.readLine()) != null){
//                sb.append(line);
//            }
//            int i = 0;
//            while(i!=-1){
//                i = bf.read();
//                sb.append((char)i);
//            }
            //Read a set of characters from the socket
            int i;
            byte[] buffer = new byte[2048];
            try{
                i = in.read(buffer);
            }catch (Exception e) {
                e.printStackTrace();
                i = -1;
            }
            for (int j = 0; j < i; j++) {
                sb.append((char)buffer[j]);
            }
            
//        }
        System.out.println(sb);
        parseUri(sb.toString());
    }
    
    private void parseUri(String sb){
        String [] lines = sb.split("\n");
        int start = sb.indexOf(" ");
        int end = sb.indexOf(" ", start + 1);
        uri = sb.substring(start + 1, end);
    }
    public String getUri(){
        return uri;
    }
    
}
